//
//  HZHeaderofSectionView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZHeaderofSectionView.h"

#define kSectionHeaderMargin 5

@interface HZHeaderofSectionView()
@property (nonatomic, strong)UIImageView *pArrow;
@property (nonatomic, strong)UILabel     *pTitle;
@property (nonatomic, strong)UIButton    *pMore;
@end

@implementation HZHeaderofSectionView

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = kMYHexToRGB(0xe8e8e8, 1);
    }
    return self;
}

- (void)p_commonInit
{
    _pArrow = [UIImageView new];
    _pTitle = [UILabel new];
    _pMore = [UIButton new];
    
    _pArrow.backgroundColor = [UIColor clearColor];
    _pTitle.backgroundColor = [UIColor clearColor];
    _pMore.backgroundColor = [UIColor clearColor];
    _pArrow.contentMode = UIViewContentModeCenter;
    
    [_pMore addTarget:self action:@selector(moreOnClick) forControlEvents:UIControlEventTouchUpInside];
    [_pMore setImage:[UIImage imageNamed:@"mark_more"] forState:UIControlStateNormal];
    
    [self addSubview:_pArrow];
    [self addSubview:_pTitle];
    [self addSubview:_pMore];
    
    [self p_initConstraints];
    
}

#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(kSectionHeaderMargin);
        make.right.mas_equalTo(_pTitle.mas_left);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(20);
    }];
    [_pTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(_pMore.mas_left);
        make.bottom.mas_equalTo(self);
    }];
    [_pMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-kSectionHeaderMargin);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(30);
    }];
}

- (void)moreOnClick
{
    DLog(@"more");
    [self.parent upToParentWithObjc:self withType:kHZ_stockListMore_Action withParamter:_pTitle.text];
}

#pragma mark -
#pragma mark public
- (void)setHeaderTitle:(NSString *)title
{
    _pTitle.text = title;
}
- (void)setIsOpen:(BOOL)isOpen
{
    _isOpen = isOpen;
    if (_isOpen) {
        _pArrow.image = [UIImage imageNamed:@"mark_down"];
    }else {
        _pArrow.image = [UIImage imageNamed:@"mark_up"];
    }
}

#pragma mark - 
#pragma mark touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self.parent upToParentWithObjc:self withType:kHZ_ClickMarketSectionHeader_Action withParamter:@(self.tag)];
}

@end
